package com.letwal.cmcvnd.mywallet

import android.annotation.SuppressLint
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.user.ReqGetUserWallet
import com.letwal.cmcvnd.base.BaseActivity
import com.letwal.cmcvnd.adata.user.ReqSetWalletName
import com.letwal.cmcvnd.aviewmodel.UserViewModel
import com.letwal.cmcvnd.components.dialog.UpdateWalletNameDialog
import com.letwal.cmcvnd.databinding.ActivityManageWalletBinding
import com.letwal.cmcvnd.enums.PageTagEnum
import com.letwal.cmcvnd.extend.*
import com.letwal.cmcvnd.utils.UserStateUtil

/** 管理币页 */
@SuppressLint("CustomSplashScreen")
class ManageWalletActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityManageWalletBinding

    //wallet id
    private var mWalletId: Int = -1

    //user viewModel
    private val mUserViewModel by lazy {
        ViewModelProvider(this)[UserViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mUserViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.my_wallet.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityManageWalletBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onContentViewBinded() {
        try {
            //监听live data 返回
            onHandleLiveDataCallback()

            //填充数据
            mActivityBinding.textManageWalletUserName.text = UserStateUtil.mMobile
//            mActivityBinding.textManageWalletAddress.text = address
//            mActivityBinding.textManageWalletName.text = name

            //点击修改wallet名称
            mActivityBinding.rlManageWalletName.setOnClickListener {
                UpdateWalletNameDialog.showDialog(
                    supportFragmentManager,
                    mActivityBinding.textManageWalletName.text.toString().trim()
                ) { newName ->
                    mActivityBinding.textManageWalletName.text = newName

                    //设置名称
                    mUserViewModel.setWalletName(
                        ReqSetWalletName(
                            name = newName,
                            walletId = mWalletId
                        )
                    )
                }
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()

        //初始化配置参数
        val coinType = intent.getStringExtra("coinType").toString()

        //获取钱包信息
        mUserViewModel.getUserWallet(ReqGetUserWallet(coinType = coinType))
    }

    /** 监听live data 返回 */
    private fun onHandleLiveDataCallback() {
        mUserViewModel.onGetUserWalletLiveData.observe(this) {
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    mWalletId = result.walletId
                    mActivityBinding.textManageWalletAddress.text = result.address
                    mActivityBinding.textManageWalletName.text = result.name
                }
            }
        }

        mUserViewModel.onSetWalletNameLiveData.observe(this) {
            if (it.isSuccess) {
                Toast.makeText(
                    this,
                    getString(R.string.manage_wallet_update_success),
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

}